package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.erpanderp.wam.adm.model.entity.CustomLookup;
import com.erpanderp.wam.adm.model.entity.LookupValue;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.service.CustomLookupDAO;
import com.erpanderp.wam.adm.model.service.ServiceDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ViewScoped
@ManagedBean

public class CreateCustomLookupValuesBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean addLookUpValue;
	private boolean FORM_VALID;
	private boolean LOOKUP_FORM_VALID;
	private CustomLookup customLookup = new CustomLookup();
	private LookupValue lookupValue = new LookupValue();
	CustomLookupDAO customLookupDAO = new CustomLookupDAO();
	private List<LookupValue> lookupValues = new ArrayList<LookupValue>();   
	private Map<String,Long> customLookupsMap = new HashMap<String, Long>();
	private List<CustomLookup>  customLookupList = new ArrayList<CustomLookup>();
	private long selectedCustomLookupId;
	
	public CreateCustomLookupValuesBean(){
		customLookupList = new CustomLookupDAO().getAllCustomLookups();
		for(CustomLookup customLookup : customLookupList){
			customLookupsMap.put(customLookup.getDisplayName(), customLookup.getCustomLookId());
		}
	}
		
	
	public void toggleAddLookupDisplay(ActionEvent e) {           
	
			System.out.println("toggleAddLookupDisplay");
			validateForm();
			if(FORM_VALID){
				setAddLookUpValue(true);	
			}
			
	}
	public void saveCustomLookup() {
		System.out.println("saveCustomLookup");
		validateForm();
		System.out.println("lookupValues.size()========="+lookupValues.size());
		
		if(lookupValues.size() == 0){
		     createAnotherLookupValueAction();
		}
		if (FORM_VALID  && lookupValues.size() >0) {			
			
			customLookupDAO.createCustomLookupValues(lookupValues);
			setAddLookUpValue(false);
			FacesContext.getCurrentInstance().addMessage(
					null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Look up values created for "+customLookup.getCustomLookupName()
							+ " lookup successfully.", ""));
			customLookup = new CustomLookup();
			lookupValue = new LookupValue();
			lookupValues.clear();
		}
		
	}

	
	
	public String createAnotherLookupValueAction() {
		System.out.println("createAnotherResourceAction...............");
		anothervalidateForm();
		
		if (LOOKUP_FORM_VALID) {
			 customLookup = new CustomLookupDAO().getCustomLookUpById(customLookup.getCustomLookId());
			lookupValue.setCreateDate(new Date());
			lookupValue.setCustomLookup(customLookup);
			lookupValue.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			lookupValue.setStatus(CommonLookup.STATUS_ACTIVE);           
			lookupValues.add(lookupValue);
			lookupValue = new LookupValue();
		}

		return null;
	}
	
	
	
	
	public void validateForm(){
		FORM_VALID = true;
		if (ValidationUtil.isEmpty((customLookup.getCustomLookId() != 0) ? String.valueOf(customLookup.getCustomLookId()):null, "Custom Lookup",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup  is empty");
			FORM_VALID = false;
		}
		
   }	
	
	
	
	public void anothervalidateForm(){
		LOOKUP_FORM_VALID =true;
		if (ValidationUtil.isEmpty(lookupValue.getLookupValue(), "Lookup value",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup Name is empty");
			LOOKUP_FORM_VALID = false;
		}
		
		
		if (ValidationUtil.isEmpty(lookupValue.getLookupName(), "Lookup Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Lookup Name");
			LOOKUP_FORM_VALID = false;
		}
		
	}
	
	
	
/*	private void validateForm() {
		FORM_VALID = true;

		if (ValidationUtil.isEmpty(customLookup.getCustomLookupName(), "Custom Lookup Name",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup Name is empty");
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(customLookup.getDisplayName(), "Display Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Display Name is empty");
			FORM_VALID = false;
		}

		if (ValidationUtil.isEmpty(lookupValue.getLookupValue(), "Lookup value",
				null, FacesContext.getCurrentInstance()) != null) {

			System.out.println("Custom Lookup Name is empty");
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(lookupValue.getLookupName(), "Lookup Name",
				null, FacesContext.getCurrentInstance()) != null) {
			System.out.println("Lookup Name");
			FORM_VALID = false;
		}

	}
	*/
	

	
	
	//Getter Setter Methods
	
	public boolean isAddLookUpValue() {
		return addLookUpValue;
	}

	public void setAddLookUpValue(boolean addLookUpValue) {
		this.addLookUpValue = addLookUpValue;
	}

	public CustomLookup getCustomLookup() {
		return customLookup;
	}

	public void setCustomLookup(CustomLookup customLookup) {
		this.customLookup = customLookup;
	}

	public LookupValue getLookupValue() {
		return lookupValue;
	}

	public void setLookupValue(LookupValue lookupValue) {
		this.lookupValue = lookupValue;
	}

	public List<LookupValue> getLookupValues() {
		return lookupValues;
	}

	public void setLookupValues(List<LookupValue> lookupValues) {
		this.lookupValues = lookupValues;
	}

	public boolean isLOOKUP_FORM_VALID() {
		return LOOKUP_FORM_VALID;
	}

	public void setLOOKUP_FORM_VALID(boolean lOOKUP_FORM_VALID) {
		LOOKUP_FORM_VALID = lOOKUP_FORM_VALID;
	}

	public Map<String,Long> getCustomLookupsMap() {
		return customLookupsMap;
	}

	public void setCustomLookupsMap(Map<String,Long> customLookupsMap) {
		this.customLookupsMap = customLookupsMap;
	}


	public List<CustomLookup> getCustomLookupList() {
		return customLookupList;
	}


	public void setCustomLookupList(List<CustomLookup> customLookupList) {
		this.customLookupList = customLookupList;
	}


	public long getSelectedCustomLookupId() {
		return selectedCustomLookupId;
	}


	public void setSelectedCustomLookupId(long selectedCustomLookupId) {
		this.selectedCustomLookupId = selectedCustomLookupId;
	}
}
